package edu.pku.sei.transformation.editor.views.editor;

import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.Path;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.FileStoreEditorInput;
import org.eclipse.ui.ide.IDE;

public class TextEditor {
	//come from IDEWorkbenchPlugin.DEFAULT_TEXT_EDITOR_ID
	public static final String DEFAULT_TEXT_EDITOR_ID = "org.eclipse.ui.DefaultTextEditor";
	
	public static void openInDefaultTextEditor(String address) throws PartInitException {
		IWorkbenchWindow dwindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();  
		IWorkbenchPage wbPage = dwindow.getActivePage();
		IFileStore file = EFS.getLocalFileSystem().getStore(new Path(address));
		FileStoreEditorInput input = new FileStoreEditorInput(file);
		wbPage.openEditor(input, DEFAULT_TEXT_EDITOR_ID);
	}
	
	public static void openInEditor(IFile file) throws PartInitException {
		IWorkbenchWindow dwindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();  
		IWorkbenchPage wbPage = dwindow.getActivePage();
		IDE.openEditor(wbPage, file);
	}
}
